package cn.szabadsag.base.controller;

import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.FastDfsUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传公共类
 *
 * @author yangcong
 * @date 2022年03月09日 13:13
 * @since v1.0
 */
@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    /**
     * 入驻请求
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        //获取文件后缀名字
        //获取文件的原始名字
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String extName = originalFilename.substring(originalFilename.indexOf(".") + 1);
        //文件的byte[]数组
        try {
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return  AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
}
